<template>
  <div >
    <el-row v-loading="loading">
      <p>上传或下载一个 Maven</p>

      <el-col :span="15">
        <el-input size="small" v-model="netUrl" clearable placeholder="输入 Maven 下载地址">
          <el-button slot="append" type="text" size="small" @click="downloadNetMaven">网络下载</el-button>
        </el-input>
      </el-col>
     <el-col :span="9">
       <el-upload style="display: inline-block" class="margin-left" action="#" :show-file-list="false" :http-request="uploadMaven">
         <el-button slot="trigger" size="small" icon="el-icon-plus" type="primary">本地上传</el-button>
       </el-upload>
     </el-col>

    </el-row>

    <el-table
      :data="localMavens"
      size="mini"
      height="600"
      border
      stripe
    >
      <el-table-column
        type="index"
        width="50"/>
      <el-table-column
        prop="name"
        label="版本号"
      />
      <el-table-column
        prop="size"
        label="文件大小"
      >
        <template slot-scope="{row}">
          {{formatSizeHuman(row.size)}}
        </template>
      </el-table-column>
      <el-table-column
        prop="lastUpdateTime"
        label="下载时间"
      >
        <template slot-scope="{row}">
          <span>{{parseTime(row.lastUpdateTime)}}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        fixed="right"
      >
        <template slot-scope="{row}">
          <el-button type="text" :disabled="current === row.name" size="small" @click="setDefault(row)" icon="el-icon-config">设置默认</el-button>
          <el-button type="text" :disabled="current === row.name" size="small" @click="dropVersion(row)" icon="el-icon-delete">删除</el-button>
        </template>

      </el-table-column>
    </el-table>

  </div>
</template>

<script>

import maven from '@/api/maven'
import {parseTime,formatSizeHuman} from '@/utils'

export default {
  name: 'LocalMavenManage',
  data(){
    return {
      localMavens: [],
      current: null,
      netUrl: null,
      loading: false
    }
  },
  methods: {
    parseTime,formatSizeHuman,
    versions(){
      maven.localMavenManager.versions().then(res => {
        this.localMavens = res.data
      })
    },
    currentMaven(){
      maven.localMavenManager.current().then(res => {
        this.current = res.data
      })
    },
    setDefault(row){
      maven.localMavenManager.setDefault(row.name).then(res => {
        this.currentMaven()
      })
    },
    dropVersion(row){
      maven.localMavenManager.deleteVersion(row.name).then(res => {
        this.versions()
      })
    },
    downloadNetMaven(){
      this.loading = true
      maven.localMavenManager.downloadNetMaven(this.netUrl).then(res => {
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    uploadMaven(data) {
      const file = data.file
      const formData = new FormData()
      formData.append('file', file, file.name)
      maven.localMavenManager.upload(formData).then(res => {
        this.versions()
      })
    },
  },
  mounted() {
    this.versions()
    this.currentMaven()
  }
}
</script>

<style scoped>

</style>
